namespace IMPlant.Core.Web.Control.GridViewFunction
{
    using IMPlant.Core.Web.Control;
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;

    public class ComplexHeadFunction : ExtendFunction
    {
        public ComplexHeadFunction()
        {
        }

        public ComplexHeadFunction(IMPlant.Core.Web.Control.GridView grid) : base(grid)
        {
        }

        private void _grid_RowCreatedDataRow(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                string str = HttpContext.Current.Server.MapPath(base._grid.ComplexHead.XmlFilePath);
                if (!string.IsNullOrEmpty(str))
                {
                    int rowIndex = 0;
                    XmlDocument document = new XmlDocument();
                    document.Load(str);
                    XmlNodeList list = document.SelectNodes("/Head/Row");
                    if (list.Count > 0)
                    {
                        e.Row.Cells.Clear();
                        e.Row.Style.Add(HtmlTextWriterStyle.Display, "none");
                        foreach (XmlNode node in list)
                        {
                            XmlNodeList childNodes = node.ChildNodes;
                            GridViewRow child = new GridViewRow(rowIndex, rowIndex, DataControlRowType.Header, DataControlRowState.Normal);
                            foreach (XmlNode node2 in childNodes)
                            {
                                TableHeaderCell tc = new TableHeaderCell();
                                if (string.Equals(node2.Attributes["AllowSorting"].Value, "true", StringComparison.OrdinalIgnoreCase))
                                {
                                    LinkButton button = new LinkButton();
                                    button.Text = node2.Attributes["Text"].Value;
                                    button.CommandArgument = node2.Attributes["BindName"].Value;
                                    button.CommandName = "Sort";
                                    button.ID = node2.Attributes["BindName"].Value;
                                    tc.Controls.Add(button);
                                    this.DisplaySortTip(base._grid.SortExpression, tc);
                                }
                                else
                                {
                                    tc.Text = node2.Attributes["Text"].Value;
                                }
                                tc.ColumnSpan = int.Parse(node2.Attributes["ColSpan"].Value);
                                tc.RowSpan = int.Parse(node2.Attributes["RowSpan"].Value);
                                child.Cells.Add(tc);
                                child.Visible = true;
                            }
                            base._grid.Controls[0].Controls.AddAt(rowIndex, child);
                            rowIndex++;
                        }
                    }
                }
            }
        }

        protected void DisplaySortTip(string sortExpression, TableCell tc)
        {
            string[] array = sortExpression.Split(new char[] { ',' });
            if ((tc.Controls.Count > 0) && (tc.Controls[0] is LinkButton))
            {
                string columnName = ((LinkButton) tc.Controls[0]).CommandArgument;
                int index = Array.FindIndex<string>(array, delegate (string s) {
                    return s.IndexOf(columnName) != -1;
                });
                if (index != -1)
                {
                    SortDirection ascending = SortDirection.Ascending;
                    if (array[index].IndexOf(DefaultSorting.DESC.ToString()) != -1)
                    {
                        ascending = SortDirection.Descending;
                    }
                    else if (array[index].IndexOf(DefaultSorting.ASC.ToString()) != -1)
                    {
                        ascending = SortDirection.Ascending;
                    }
                    else if (base._grid.SortDirection == SortDirection.Descending)
                    {
                        ascending = SortDirection.Descending;
                    }
                    string sortAscImageUrl = null;
                    string text = null;
                    if (ascending == SortDirection.Ascending)
                    {
                        text = base._grid.SmartSorting.SortAscText;
                        sortAscImageUrl = base._grid.SmartSorting.SortAscImageUrl;
                        if (string.IsNullOrEmpty(sortAscImageUrl))
                        {
                            sortAscImageUrl = base._grid.Page.ClientScript.GetWebResourceUrl(base.GetType(), "IMPlant.Core.Web.UI.Image.Asc.gif");
                        }
                    }
                    else
                    {
                        text = base._grid.SmartSorting.SortDescText;
                        sortAscImageUrl = base._grid.SmartSorting.SortDescImageUrl;
                        if (string.IsNullOrEmpty(sortAscImageUrl))
                        {
                            sortAscImageUrl = base._grid.Page.ClientScript.GetWebResourceUrl(base.GetType(), "IMPlant.Core.Web.UI.Image.Desc.gif");
                        }
                    }
                    Image child = new Image();
                    child.ImageUrl = sortAscImageUrl;
                    tc.Controls.Add(child);
                    tc.Controls.Add(new LiteralControl(text));
                    if (base._grid.SmartSorting.AllowMultiSorting)
                    {
                        int num2 = index + 1;
                        tc.Controls.Add(new LiteralControl(num2.ToString()));
                    }
                }
            }
        }

        protected override void Execute()
        {
            base._grid.RowCreatedDataRow += new IMPlant.Core.Web.Control.GridView.RowCreatedHandler(this._grid_RowCreatedDataRow);
        }
    }
}

